<?php
namespace app\admin\model;

/*邮寄模型*/
use app\admin\model\CommonModel;

class Mail extends CommonModel
{
    private $table_name = 'mail';
    public function __construct()
    {
       parent::__construct();
    }
    /**初始化参数
     * @param $args [client_id,token,enc_string,nickname]
     * @return array
     */
    private function default_param($args)
    {
        $default = [
            "title"     => "",
            "content"   => "",
            "add_time"  => time(),
            "is_read"   => 0,
            "phone"     => ""
        ];
        return array_merge($default,$args);
    }


    //获取所有自动回复
    public function all($args = [])
    {
        /**
         * $args参数说明
         * args.id 根据回复id
         * args.keyword 根据title模糊查询
         * pageLimit 每页显示多少条数据 默认15条
         */

        $pageLimit = isset($args['pageLimit']) && intval($args['pageLimit']) > 0 ? $args['pageLimit'] : 15;
        $where = [];
        if(isset($args['id']) && intval($args['id']) > 0)
        {
            $where['id'] = intval($args['id']);
        }

        if(isset($args['keyword']) && $args['keyword'])
        {
            $where['title'] = ['like','%'.$args['keyword'].'%'];
        }
        //获取数据
        $data = null;
        if(!empty($where))
        {
            $data = db($this->table_name)->where($where)->order('add_time DESC, id DESC')->paginate($pageLimit);
        }
        else
        {
            $data = db($this->table_name)->order('add_time DESC, id DESC')->paginate($pageLimit);
        }

        return [
            'data' => $data,
            'page' => $data->render()
        ];
    }

    /**获取一个信
     * @param array $args key:[id], 中括号表示任选一个可以查询
     * @return bool
     */
    public function info($args)
    {

        if(empty($args)) return false;
        //参数合并
        $args = $this->default_param($args);
        //如果没有查询信息，返回空
        if((!isset($args['id']) || empty($args['id'] ))
        ) return false;

        //根据id,title,content其中一个查询
        if(!empty($args['id']))
        {
            $filter = ['id'=>$args['id']];
        }
        if(empty($filter)) return false;

        //返回查询信息
        return  db($this->table_name)->where($filter)->find();
    }

    //删除信封
    public function del($args)
    {
        /**
         * args参数说明
         * args为一个数组，包含所有的id值
         */
        if(empty($args))
        {
            return $this->returnState(1,"没有信封被删除");
        }
        db($this->table_name)->where(['id'=>['in',implode(',',$args)]])->delete();
        return $this->returnState(0,"删除完毕");
    }

    //设置已读状态
    public function read($id)
    {
        if(is_string($id))
        {
            $id = ['id'=>[$id]];
        }
        if(!isset($id['id']) || empty($id['id']))
        {
            return $this->returnState(1,"没有选中邮件");
        }
        $id = implode($id['id'],',');
        db($this->table_name)->where(['id'=>['in',$id]])->update(['is_read'=>1]);
        return $this->returnState(0,"设置已读状态完毕");
    }

    //统计邮件个数
    public function countAll($no_read = false)
    {
        $where = [];
        if($no_read)
        {
            $where['is_read'] = 0;
        }
        return db($this->table_name)->where($where)->count();
    }

}
